import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qaun_ni_dev/pages/attribute/attribute_update.dart';
import 'package:qaun_ni_dev/utils/date_time.dart';
import 'package:qaun_ni_dev/provider/attributeProvide.dart';


class AttributeDetail extends StatelessWidget {
  var attributeData;
  AttributeDetail(this.attributeData);


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('属性详情'),
      ),
        body: Stack(
          children: <Widget>[
            _detailContent(attributeData),
            _optionBtn(context, attributeData),
          ],
        )
    );
  }

  Widget _detailContent(attributeData) {
    return Container(
        padding: EdgeInsets.only(top: 10, left: 10, right: 10, bottom: 80),
        height: 610,
        child: ListView(
          children: <Widget>[
            Column(
              children: <Widget>[
                Container(
                  padding:
                  EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('数据id:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${attributeData.id}'),
                      )
                    ],
                  ),
                ),
                Container(
                  padding:
                  EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('属性code:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${attributeData.code}'),
                      )
                    ],
                  ),
                ),
                Container(
                  padding:
                  EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('描述:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${attributeData.description}'),
                      )
                    ],
                  ),
                ),
                Container(
                  padding:
                  EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('键值:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${attributeData.val}'),
                      )
                    ],
                  ),
                ),
                Container(
                  padding:
                  EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('属性type:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${attributeData.type}'),
                      )
                    ],
                  ),
                ),
                Container(
                  padding:
                  EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('创建时间:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${timeStampChange(attributeData.createTime)}'),
                      )
                    ],
                  ),
                ),

              ],
            )
          ],
        ));
  }

  Widget _optionBtn(context, attributeData) {
    return Positioned(
      bottom: 0,
      width: 360,
      height: 80,
      left: 40,
      child: Container(
        alignment: Alignment.center,
        child: Row(
          children: <Widget>[
            Container(
              width: 100,
              height: 40,
              padding: EdgeInsets.only(top: 10, bottom: 10),
              margin: EdgeInsets.only(right: 80),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(40)),
                color: Colors.lightBlue,
              ),
              child: InkWell(
                onTap: () {
                  print('编辑');
                  Navigator.push(context, MaterialPageRoute(builder: (context) {
                    return AttributeUpdate(attributeData);
                  }));
                },
                child: Text(
                  '编辑',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
            Container(
              width: 100,
              height: 40,
              padding: EdgeInsets.only(top: 10, bottom: 10),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(40)),
                color: Colors.redAccent,
              ),
              child: InkWell(
                onTap: () {
                  _deleteAttribute(context, attributeData);
                },
                child: Text(
                  '删除',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _deleteAttribute(context, attributeData) {
    showDialog<Null>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('警告'),
          content: Text('确认要删除该条数据吗？'),
          actions: <Widget>[
            FlatButton(
              child: Text("取消"),
              onPressed: () {
                Navigator.of(context).pop();
              } , //关闭对话框
            ),
            FlatButton(
              child: Text("确认"),
              onPressed: () {
                Provider.of<AttributeProvide>(context,listen: false).deleteAttribute(context,attributeData.id);
                Navigator.of(context).pop(); //关闭对话框
              },
            ),
          ],
        );
      },
    ).then((val) {
      print(val);
    });
  }
}





